也有英文版文章
Also this tutorial has been written in English
Check out my Medium
結構體(Struct)和枚舉(Enum)是兩個不可分割的存在,今天終於介紹到枚舉(Enum)。
一個枚舉的值可以是任何可能的變量之一。
" Where structs give you a way of grouping together related fields and data, like a Rectangle with its width and height, enums give you a way of saying a value is one of a possible set of values. "- Book of The Rust Programming Language
Again, 枚舉中的資料只是無數可能性的其中之一。
enum Direction {
    Left,
    Right,
    Up,
    Down,
}
fn main() {
    let go = Direction::Up;
    match go {
        Direction::Up => println!("上!"),
        Direction::Left => println!("左!"),
        Direction::Right => println!("右!"),
        Direction::Down => println!("下!"),
    }
}
/* 
上!
*/
enum Colour {
    Red,
    Yellow,
    Blue,
}
fn print_color(my_colour: Colour) {
    match my_colour {
        Colour::Red => println!("red"),
        Colour::Yellow => println!("yellow"),
        Colour::Blue => println!("blue"),
    }
}
fn main() {
    print_color(Colour::Blue);
}
接續昨天的例子,今天結合了枚舉(Enum)和結構體(Struct),初步了解如何使用這些工具。
// 口味
enum Flavour {
    Sweet,
    Sparkling,
    Fruity,
}
// 飲料口味 + 盎司
struct Drink {
    flavour: Flavour,
    fluid_oz: f64,
}
// 輸出調飲資訊(data)
fn print_drinks(the_drink: Drink) {
    match the_drink.flavour {
        Flavour::Sweet => println!("Sweet Drink!"),
        Flavour::Sparkling => println!("Sparkling Drink!"),
        Flavour::Fruity => println!("Fruity Drink!"),
    }
    println!("Oz: {:?}", the_drink.fluid_oz);
}
// Driver code
fn main() {
    let a: Drink = Drink {
        flavour: Flavour::Sparkling,
        fluid_oz: 10.5,
    };
    print_drinks(a);
}
let (x, y, z) = (1, 2, 3);
BTW.
這個未來會再提到,不過其實你已經身在Rust細思極恐的包圍網設計概念中了...
let PATTERN = EXPRESSION;
fn coordinate() -> (i32, i32) {
    (1, 7)
}
fn main() {
    let (x, y) = coordinate();
    if y > 5 {
        println!(">5");
    } else if y < 5 {
        println!("<5");
    } else {
        println!("=5");
    }
}
/*
output:
5
*/